package com.xingda.packing.dmds.minitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xingda.packing.dmds.common.R;
import com.xingda.packing.dmds.enums.ActionFinishState;
import com.xingda.packing.dmds.minitor.entity.PkAction;
import com.xingda.packing.dmds.minitor.service.IPkActionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController("pkActionDMDSController")
@RequestMapping("/api/action")
public class PkActionController {


    @Resource
    IPkActionService pkActionService;

    @GetMapping("/getPage")
    public R getPage(Integer actionType,
                     Integer finishState,
                     Integer area,
                     Long current,
                     Long size) {
        try {
            IPage<PkAction> pkEventsPage = this.pkActionService.getList(actionType, finishState, area, current, size);
            return R.ok().put("data", pkEventsPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/modify")
    public R modifyById(Integer id,
                        Integer finishState) {
        try {
            if (finishState == null) {
                return R.error("无完成状态--finishState！");
            }
            if (finishState != ActionFinishState.FINISH.getNumber() && finishState != ActionFinishState.IGNORE.getNumber()) {
                return R.error("完成状态数据错误--finishState！");
            }

            PkAction pkAction = pkActionService.getById(id);
            if (pkAction == null) {
                return R.error("无此id动作信息--id！");
            }

            if (pkAction.getFinishState() != ActionFinishState.START.getNumber()) {
                return R.error("此动作信息不是开始状态！");
            }

            pkActionService.modifyById(id, finishState);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

}
